package tw.twandroidproj.app.ui.SelectTime;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.prolificinteractive.materialcalendarview.CalendarDay;
import com.prolificinteractive.materialcalendarview.MaterialCalendarView;
import com.prolificinteractive.materialcalendarview.OnDateSelectedListener;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import tw.twandroidproj.app.R;
import tw.twandroidproj.app.ui.Apply.job_apply;
import tw.twandroidproj.app.ui.activity.details_job_Activity;

public class selectTime extends AppCompatActivity implements View.OnClickListener {

    private static final DateFormat FORMATTER = SimpleDateFormat.getDateInstance();
    MaterialCalendarView calendar_time;
    MyGridView hour_gridView;
    CheckBox calendarCheck;
    gridAdapter gridadapter;
    Button addTime;
    Button gridNext;
    Button comproary;
    Button timeClear;
    TextView dateShow;
    String dateSelected = "";
    String timeSelected = "";
    Editable dateText;
    List<CalendarDay> dateSelectedList = new ArrayList<>();
    int temp = 0;//标识子Layout是否已添加
    private LinearLayout mLinearLayout;
    private View mGridView;// 子Layout要以view的形式加入到主Layout中
    int timeSelectedTag = 0;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.timeselect);

        mGridView = View.inflate(this, R.layout.timeselect_gridview, null);
        mLinearLayout = (LinearLayout) findViewById(R.id.box);
        hour_gridView = (MyGridView) mGridView.findViewById(R.id.hour_gridView);
        comproary = (Button) mGridView.findViewById(R.id.comproary);
        timeClear = (Button) mGridView.findViewById(R.id.timeClear);
        dateShow = (TextView) mGridView.findViewById(R.id.detail_dateText);
        addTime = (Button) findViewById(R.id.addTime);
        gridNext = (Button) findViewById(R.id.gridNext);

        calendar_time = (MaterialCalendarView) findViewById(R.id.calendar_time);
        calendar_time.setOnDateChangedListener(new OnDateSelectedListener() {
            @Override
            public void onDateSelected(MaterialCalendarView widget, CalendarDay date, boolean selected) {

                dateSelectedList = calendar_time.getSelectedDates();
            }
        });
        gridadapter = new gridAdapter(selectTime.this);
        hour_gridView.setAdapter(gridadapter);

        dateShow.setText(dateShow.getText(), TextView.BufferType.EDITABLE);
        dateText = (Editable) dateShow.getText();

        calendarCheck = (CheckBox) findViewById(R.id.calendarCheck);
        calendarCheck.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked)
                    calendar_time.setSelectionMode(MaterialCalendarView.SELECTION_MODE_MULTIPLE);
                else
                    calendar_time.setSelectionMode(MaterialCalendarView.SELECTION_MODE_SINGLE);
            }
        });


        hour_gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                gridadapter.setSeclection(position);
                dateSelected = gridadapter.getTime();
                gridadapter.notifyDataSetChanged();
                timeSelectedTag = 1;
            }
        });

        comproary.setOnClickListener(this);
        addTime.setOnClickListener(this);
        gridNext.setOnClickListener(this);
        timeClear.setOnClickListener(this);

    }

    private void getSelectedTime() {
        timeSelected = "";
        List<String> griddata = gridadapter.getGridDataList();
        int[] gridCick =gridadapter.getClickedList();
        if (timeSelectedTag == 1) {
            for (int i = 0; i < gridCick.length; i++) {
                if (gridCick[i] == 1) {
                    timeSelected += griddata.get(i);
                }
            }
            timeSelected += "\n\n";
            timeSelectedTag = 0;
        }
    }
    private void getSelectedDates() throws IndexOutOfBoundsException{
        dateSelected = "";
        if (dateSelectedList.get(0) != null) {
            for (int i = 0; i < dateSelectedList.size(); i++) {
                if (dateSelectedList.get(i) != null) {
                    dateSelected += FORMATTER.format(dateSelectedList.get(i).getDate()) + " ";
                }
            }
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.addTime:
                if (temp == 0) {
                    mLinearLayout.addView(mGridView);
                    temp = 1;
                }
                break;
            case R.id.gridNext:
                Intent selectResume = new Intent();
                selectResume.setClass(selectTime.this, job_apply.class);
                startActivity(selectResume);
                break;
            case R.id.comproary:
                try {
                    getSelectedDates();
                    getSelectedTime();
                    dateText.append(dateSelected);
                    dateText.append("\n");
                    dateText.append(timeSelected);
                    calendar_time.clearSelection();
                }catch (IndexOutOfBoundsException ex){
                    Toast.makeText(this,"请选择日期！",Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.timeClear:
                dateText.clear();
                break;
            default:
                break;
        }
    }
}
